﻿/// Copyright 2011 - Agus Kurniawan
/// Blog: http://blog.aguskurniawan.net 
/// ====================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace Galunggung
{
    public partial class MacAddressLookup : Form
    {
        private List<NicInfo> _listMacs = null;
        public NicInfo SelectSpoofedMacAddress { set; get; }

        public MacAddressLookup()
        {
            InitializeComponent();
        }

        private void PopulateData()
        {
            dgMacs.DataSource = null;
            dgMacs.DataSource = _listMacs;
        }

        private bool Validate(string mac)
        {
            if (_listMacs == null)
                return true;

            for (int i = 0; i < _listMacs.Count; i++)
            {
                if (_listMacs[i].SpoofedMACAdress.ToLower() == mac.ToLower())
                    return false;
            }

            return true;
        }

        private void MacAddressLookup_Load(object sender, EventArgs e)
        {
            backgroundLoadData.RunWorkerAsync();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (dgMacs.SelectedRows.Count > 0)
            {
                SelectSpoofedMacAddress = (NicInfo)dgMacs.SelectedRows[0].DataBoundItem;

                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
            else
                MessageBox.Show("Please choice one MAC Address", "Information");
        }

        private void backgroundLoadData_DoWork(object sender, DoWorkEventArgs e)
        {
            SqlMacAddress data = SqlMacAddress.Create();
            _listMacs = data.GetAllMacs();
        }

        private void backgroundLoadData_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PopulateData();
        }

        private void btnAddAutogenerate_Click(object sender, EventArgs e)
        {
            //auto
            List<string> macs = Helper.GenerateAutoMacAddress();
            txtMac1.Text = macs[0];
            txtMac2.Text = macs[1];
            txtMac3.Text = macs[2];
            txtMac4.Text = macs[3];
            txtMac5.Text = macs[4];
            txtMac6.Text = macs[5];
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string mac = string.Format("{0}-{1}-{2}-{3}-{4}-{5}", txtMac1.Text,
                txtMac2.Text, txtMac3.Text, txtMac4.Text, txtMac5.Text, txtMac6.Text);

            if (string.IsNullOrEmpty(txtMac1.Text) || string.IsNullOrEmpty(txtMac2.Text) ||
                string.IsNullOrEmpty(txtMac3.Text) || string.IsNullOrEmpty(txtMac4.Text) ||
                string.IsNullOrEmpty(txtMac5.Text) || string.IsNullOrEmpty(txtMac6.Text))
            {
                MessageBox.Show("Please write correct MAC Address", "Information");
                return; 
            }

            if (!Helper.IsValidHexString(txtMac1.Text) || !Helper.IsValidHexString(txtMac2.Text) ||
                !Helper.IsValidHexString(txtMac3.Text) || !Helper.IsValidHexString(txtMac4.Text) ||
                !Helper.IsValidHexString(txtMac5.Text) || !Helper.IsValidHexString(txtMac6.Text))
            {
                MessageBox.Show("Mac Address is invalid", "Information");
                return;                    
            }

            if (!Validate(mac))
            {
                MessageBox.Show("Mac Address already have entered", "Information");
                return;
            }
            
            try
            {
                SqlMacAddress data = SqlMacAddress.Create();
                data.InsertMacAddress(mac);
                _listMacs = data.GetAllMacs();
                PopulateData();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Error");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgMacs.SelectedRows.Count > 0)
            {
                try
                {
                    SqlMacAddress data = SqlMacAddress.Create();
                    for (int i = 0; i < dgMacs.SelectedRows.Count; i++)
                    {
                        NicInfo o = (NicInfo)dgMacs.SelectedRows[i].DataBoundItem;
                        data.DeleteMacAddress(o.SpoofedMACAdressId);
                    }

                    _listMacs = data.GetAllMacs();
                    PopulateData();
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message, "Error");
                }
            }
        }
    }
}
